﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Cms.Model;
using Portal.Web.Model;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Cms.Utility;
using Portal.Core;

namespace Portal.Cms.Render
{
    public class GroupDataFolderRenderBll
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(GroupDataId)) throw new Exception("Tham số không hợp lệ!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                if (OGroupData == null) throw new Exception("Tham số không hợp lệ!");
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, GroupDataId, WebSessionUtility.GetCurrentLoginUser().UserId, true);

                GroupDataFolderCls[] GroupDataFolders =
                    CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataFolder(null, SiteId, GroupDataId, null);
                
                string Html =
                              "<input id=\"txtGroupDataId\" type=\"hidden\" value=\""+GroupDataId+"\">\r\n"+
                              "<script language=javascript>\r\n"+
                              "     function ClientAddGroupDataFolder()\r\n"+
                              "     {\r\n"+
                              "         SiteId='"+SiteId+"';\r\n"+
                              "         ParentGroupDataFolderId='';\r\n"+
                              "         var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                              "         if(SelectedNode==null)\r\n"+
                              "         {\r\n"+
                              "             alert('Chưa chọn vào thư mục!');\r\n"+
                              "             return;\r\n"+
                              "         }\r\n"+
                              "         ParentGroupDataFolderId=SelectedNode.data.href;\r\n" +
                              "         if(ParentGroupDataFolderId==null)ParentGroupDataFolderId='';\r\n" +
                              "         AjaxOut = Portal.Cms.Render.GroupDataFolderRenderBll.ServerSideGetAddGroupDataFolderForm(SiteId,ParentGroupDataFolderId).value;\r\n"+
                              "         document.getElementById('divFormAction').innerHTML=AjaxOut.HtmlContent;\r\n"+
                              "         document.getElementById('divListForm').style.display='none';\r\n" +
                              "         document.getElementById('divFormAction').style.display='block';\r\n" +
                              "         document.getElementById('txtFolderName').focus();\r\n" +
                              "     }\r\n"+

                              "     function ClientMoveGroupDataFolder()\r\n"+
                              "     {\r\n"+
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         MoveGroupDataFolderId='';\r\n" +
                              "         var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                              "         if(SelectedNode==null)\r\n" +
                              "         {\r\n" +
                              "             alert('Chưa chọn vào thư mục!');\r\n" +
                              "             return;\r\n" +
                              "         }\r\n" +
                              "         MoveGroupDataFolderId=SelectedNode.data.href;\r\n" +
                              "         if(MoveGroupDataFolderId==null)\r\n"+
                              "         {\r\n"+
                              "             alert('Chọn vào thư mục cần chuyển!');\r\n"+
                              "             return;\r\n"+
                              "         }\r\n"+
                              "         window.open('MoveGroupDataFolder.aspx?gid=" + GroupDataId + "&MoveGroupDataFolderId='+MoveGroupDataFolderId,'_self');\r\n" +
                              "     }\r\n"+


                              "     function ClientUpdateGroupDataFolder()\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                              "         if(SelectedNode==null)\r\n" +
                              "         {\r\n" +
                              "             alert('Chưa chọn vào thư mục!');\r\n" +
                              "             return;\r\n" +
                              "         }\r\n" +
                              "         GroupDataFolderId=SelectedNode.data.href;\r\n" +
                              "         if(GroupDataFolderId==null)\r\n"+
                              "         {\r\n"+
                              "             alert('Thư mục gốc không sửa được!');\r\n"+
                              "             return;\r\n"+
                              "         }\r\n"+
                              "         AjaxOut = Portal.Cms.Render.GroupDataFolderRenderBll.ServerSideGetUpdateGroupDataFolderForm(SiteId,GroupDataFolderId).value;\r\n" +
                              "         document.getElementById('divFormAction').innerHTML=AjaxOut.HtmlContent;\r\n" +
                              "         document.getElementById('divListForm').style.display='none';\r\n" +
                              "         document.getElementById('divFormAction').style.display='block';\r\n" +
                              "         document.getElementById('txtFolderName').focus();\r\n" +
                              "     }\r\n" +

                              "     function ClientBack()\r\n"+
                              "     {\r\n"+
                              "         document.getElementById('divListForm').style.display='block';\r\n" +
                              "         document.getElementById('divFormAction').style.display='none';\r\n" +
                              "         document.getElementById('divFormAction').innerHTML='';\r\n"+
                              "     }\r\n"+

                              "     function ClientSaveAddGroupDataFolder(ParentGroupDataFolderId)\r\n" +
                              "     {\r\n"+
                              "         SiteId='"+SiteId+"';\r\n"+
                              "         GroupDataId=document.getElementById('txtGroupDataId').value;\r\n" +
                              "         FolderName=document.getElementById('txtFolderName').value;\r\n"+
                              "         SortIndex=document.getElementById('txtSortIndex').value;\r\n" +
                              "         AjaxOut = Portal.Cms.Render.GroupDataFolderRenderBll.ServerSideAddGroupDataFolder(SiteId, GroupDataId, ParentGroupDataFolderId, FolderName, SortIndex).value;\r\n" +
                              "         if(AjaxOut.Error)\r\n"+
                              "         {\r\n"+
                              "             alert(AjaxOut.InfoMessage);\r\n"+
                              "             return;\r\n"+
                              "         }\r\n"+
                              "         id=AjaxOut.RefKeyId;\r\n" +
                              "         var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                              "         var childNode=null;\r\n" +
                              "         if(SelectedNode!=null)\r\n" +
                              "         {\r\n" +
                              "             childNode = SelectedNode.addChild({\r\n" +
                              "                 isFolder: true\r\n" +
                              "             });\r\n" +
                              "             childNode.data.href=id;\r\n" +
                              "             childNode.setTitle(FolderName);\r\n" +
                              "         }\r\n" +
                              "         ClientBack();\r\n"+
                              "     }\r\n"+


                              "     function ClientSaveUpdateGroupDataFolder(GroupDataFolderId)\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         GroupDataId=document.getElementById('txtGroupDataId').value;\r\n" +
                              "         FolderName=document.getElementById('txtFolderName').value;\r\n" +
                              "         SortIndex=document.getElementById('txtSortIndex').value;\r\n" +
                              "         AjaxOut = Portal.Cms.Render.GroupDataFolderRenderBll.ServerSideUpdateGroupDataFolder(SiteId, GroupDataId, GroupDataFolderId, FolderName, SortIndex).value;\r\n" +
                              "         if(AjaxOut.Error)\r\n" +
                              "         {\r\n" +
                              "             alert(AjaxOut.InfoMessage);\r\n" +
                              "             return;\r\n" +
                              "         }\r\n" +
                              "         var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                              "         var childNode=null;\r\n" +
                              "         if(SelectedNode!=null)\r\n" +
                              "         {\r\n" +
                              "             SelectedNode.setTitle(FolderName);\r\n" +
                              "         }\r\n" +
                              "         ClientBack();\r\n" +
                              "     }\r\n" +

                              "     function ClientDeleteGroupDataFolder()\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                              "         if(SelectedNode==null)\r\n" +
                              "         {\r\n" +
                              "             alert('Chưa chọn vào thư mục cần xóa!');\r\n"+
                              "             return;\r\n"+
                              "         }\r\n"+
                              "         GroupDataId=document.getElementById('txtGroupDataId').value;\r\n" +
                              "         GroupDataFolderId=SelectedNode.data.href;\r\n" +
                              "         if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n"+
                              "         AjaxOut = Portal.Cms.Render.GroupDataFolderRenderBll.ServerSideDeleteGroupDataFolder(SiteId, GroupDataId, GroupDataFolderId).value;\r\n" +
                              "         if(AjaxOut.Error)\r\n" +
                              "         {\r\n" +
                              "             alert(AjaxOut.InfoMessage);\r\n" +
                              "             return;\r\n" +
                              "         }\r\n" +
                              "         SelectedNode.remove();\r\n" +
                              "     }\r\n" +
                              "</script>\r\n"+
                              "<div id=divFormAction class=CssFormSaveUpdate style=\"display:none;\"></div>\r\n"+
                              "<div class=FormFunction id=divListForm>\r\n" +
                              " <h3>"+OGroupData.GroupDataName+"</h3>\r\n"+
                              " <div class=DatabaseFunction>\r\n" +
                              "        <ul>\r\n" +
                              "            <li onclick=\"javascript:ClientAddGroupDataFolder();\"><img class=image src=\"" + IconRoot + "/Add.png\"><div>Tạo thư mục</div></li>\r\n" +
                              "            <li onclick=\"javascript:ClientUpdateGroupDataFolder();\"><img class=image src=\"" + IconRoot + "/Update.png\"><div>Sửa thư mục</div></li>\r\n" +
                              "            <li onclick=\"javascript:ClientDeleteGroupDataFolder();\"><img class=image src=\"" + IconRoot + "/Delete.png\"><div>Xóa thư mục</div></li>\r\n" +
                              //"            <li onclick=\"javascript:ClientMoveGroupDataFolder();\"><img class=image src=\"" + IconRoot + "/Move.png\"><div>Chuyển thư mục</div></li>\r\n" +
                              "            <li onclick=\"javascript:window.open('DocumentInputData.aspx?gid="+GroupDataId+"','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                              "        </ul>\r\n" +
                              "  </div>\r\n" +
                              

                              "<div class=TreeHolder>\r\n" +
                              "<div id=\"tree\">\r\n" +
                              "<ul>\r\n" +
                              " <li class=\"folder expanded\">Thư mục gốc</a>\r\n" +
                              "     <ul>\r\n";
                    for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                    {
                        Html +=
                                "<li class=\"folder expanded\"><a href=\"" + GroupDataFolders[iIndex].GroupDataFolderId + "\" target=\"_self\">" + GroupDataFolders[iIndex].FolderName + "</a>\r\n" +
                                        BuildSubTreeView(SiteId, GroupDataId, GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent +
                                "</li>\r\n";
                    }

                    Html += "</ul>\r\n" +
                            "</li>\r\n" +
                            "</ul>\r\n" +
                            "</div>\r\n" +
                            "</div>\r\n" +
                            "</div>\r\n";
                
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(
            string SiteId,
            string GroupDataId, 
            string ParentGroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                GroupDataFolderCls[] GroupDataFolders =
                    CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataFolder(null, SiteId, GroupDataId, ParentGroupDataFolderId);
                Html+=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + GroupDataFolders[iIndex].GroupDataFolderId + "\">" + GroupDataFolders[iIndex].FolderName + "</a>\r\n" +
                                BuildSubTreeView(SiteId, GroupDataId, GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetAddGroupDataFolderForm(
            string SiteId,
            string ParentGroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam=WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                              " <div class=DatabaseFunction>\r\n" +
                              "        <ul>\r\n" +
                              "            <li onclick=\"javascript:ClientSaveAddGroupDataFolder('"+ParentGroupDataFolderId+"');\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                              "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                              "        </ul>\r\n" +
                              "  </div>\r\n" +
                                "<table>\r\n" +
                                "   <tr>\r\n" +
                                "       <td class=Col1>Tên thư mục:</td>\r\n" +
                                "       <td class=Col2><input type=textbox class=textbox id=txtFolderName></td>\r\n" +
                                "   </tr>\r\n" +
                                "   <tr>\r\n" +
                                "       <td class=Col1>Thứ tự:</td>\r\n" +
                                "       <td class=Col2><input type=textbox class=textbox id=txtSortIndex value=\"0\"></td>\r\n" +
                                "   </tr>\r\n" +
                                "</table>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetUpdateGroupDataFolderForm(
            string SiteId,
            string GroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                GroupDataFolderCls OGroupDataFolder = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataFolderModel(null, SiteId, GroupDataFolderId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                              " <div class=DatabaseFunction>\r\n" +
                              "        <ul>\r\n" +
                              "            <li onclick=\"javascript:ClientSaveUpdateGroupDataFolder('" + GroupDataFolderId + "');\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                              "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                              "        </ul>\r\n" +
                              "  </div>\r\n" +
                                "<table>\r\n" +
                                "   <tr>\r\n" +
                                "       <td class=Col1>Tên thư mục:</td>\r\n" +
                                "       <td class=Col2><input type=textbox class=textbox id=txtFolderName value=\"" + OGroupDataFolder.FolderName + "\"></td>\r\n" +
                                "   </tr>\r\n" +
                                "   <tr>\r\n" +
                                "       <td class=Col1>Thứ tự:</td>\r\n" +
                                "       <td class=Col2><input type=textbox class=textbox id=txtSortIndex value=\"" + OGroupDataFolder.SortIndex + "\"></td>\r\n" +
                                "   </tr>\r\n" +
                                "</table>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAddGroupDataFolder(
            string SiteId,
            string GroupDataId,
            string ParentGroupDataFolderId,
            string FolderName,
            string SortIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                if (string.IsNullOrEmpty(FolderName)) throw new Exception("Chưa nhập tên thư mục!");
                if (Portal.Utility.FunctionUtilities.checkInteger(SortIndex)==false) throw new Exception("Số thứ tự chưa nhập hoặc không hợp lệ!");
                string GroupDataFolderId=CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).AddGroupDataFolder(null, SiteId, GroupDataId, ParentGroupDataFolderId, FolderName, int.Parse(SortIndex));
                OAjaxOut.RefKeyId=GroupDataFolderId;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideUpdateGroupDataFolder(
            string SiteId,
            string GroupDataId,
            string GroupDataFolderId,
            string FolderName,
            string SortIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                if (string.IsNullOrEmpty(FolderName)) throw new Exception("Chưa nhập tên thư mục!");
                if (Portal.Utility.FunctionUtilities.checkInteger(SortIndex) == false) throw new Exception("Số thứ tự chưa nhập hoặc không hợp lệ!");
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).SaveGroupDataFolder(null, SiteId, GroupDataId, GroupDataFolderId, FolderName, int.Parse(SortIndex));
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetUpdateGroupDataFolder(
            string SiteId,
            string GroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                //OAjaxOut =
                  //  GroupDataFolderRenderBll.DrawUpdateGroupDataFolderForAjax(SiteId, GroupDataFolderId, "javascript:BackFromUpdateGroupDataFolder();");
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
        
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteGroupDataFolder(
            string SiteId,
            string GroupDataId,
            string GroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).DeleteGroupDataFolder(null, SiteId, GroupDataId, GroupDataFolderId);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = "Cần xóa thư mục con trước hoặc đã tồn tại văn bản thuộc thư mục nên không xóa được!";
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
